package com.flyou.ccnucenter.ui.activity.account;

import android.app.ProgressDialog;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.flyou.ccnucenter.R;
import com.flyou.ccnucenter.domain.MyUser;
import com.flyou.ccnucenter.ui.activity.BaseActivity1;
import com.flyou.ccnucenter.utils.BmobErrorCodeUtils;
import com.flyou.ccnucenter.utils.Md5Parser;
import com.flyou.ccnucenter.utils.RegexPatternUtils;
import com.flyou.ccnucenter.utils.SPUtils;
import com.flyou.ccnucenter.widget.ClearEditText;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;


public class ChangePasswordActivity extends BaseActivity1 implements View.OnClickListener {
    private RelativeLayout mTitlebarRlBack;
    private TextView mTitlebarTvTitle;
    private TextView mTitlebarTvTextRight;
    private ClearEditText etOldPassword;
    private ClearEditText etNewPassword;
    private ClearEditText etReNewPassword;


    @Override
    protected void initView() {
        setContentView(R.layout.activity_change_password);
        mTitlebarRlBack = (RelativeLayout) findViewById(R.id.titlebar_rl_back);
        mTitlebarTvTitle = (TextView) findViewById(R.id.titlebar_tv_title);
        mTitlebarTvTextRight = (TextView) findViewById(R.id.titlebar_tv_text_right);


        etOldPassword = (ClearEditText) findViewById(R.id.et_old_password);
        etNewPassword = (ClearEditText) findViewById(R.id.et_new_password);
        etReNewPassword = (ClearEditText) findViewById(R.id.et_re_new_password);

    }

    @Override
    protected void initDate() {
        mTitlebarTvTitle.setText("修改密码");
        mTitlebarTvTextRight.setVisibility(View.VISIBLE);
        mTitlebarTvTextRight.setText("保存");


    }

    @Override
    protected void setListener() {
        mTitlebarRlBack.setOnClickListener(this);
        mTitlebarTvTextRight.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.titlebar_rl_back:
                finish();
                break;
            case R.id.titlebar_tv_text_right:
                // TODO: 2015/10/28  提交旧密码和新密码 手机号，验证码到服务器 返回是否修改成功
                if (etOldPassword.getText().toString().trim().isEmpty()) {
                    Toast.makeText(this, "旧密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (etOldPassword.getText().toString().trim().isEmpty() || etReNewPassword.getText().toString().trim().isEmpty()) {
                    Toast.makeText(this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!etNewPassword.getText().toString().trim().equals(etReNewPassword.getText().toString().trim())) {
                    Toast.makeText(this, "新密码输入不一致", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!RegexPatternUtils.isPassword(etOldPassword.getText().toString().trim())) {
                    Toast.makeText(this, R.string.regex_password_error, Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!RegexPatternUtils.isPassword(etNewPassword.getText().toString().trim())) {
                    Toast.makeText(this, R.string.regex_password_error, Toast.LENGTH_SHORT).show();
                    return;
                }
                mProgressDialog = ProgressDialog.show(ChangePasswordActivity.this, null, "正在保存密码", false, false);
                String md5OldPassword = Md5Parser.md5Parser(etOldPassword.getText().toString().trim());
                final String Md5EtReNewPassword = Md5Parser.md5Parser(etNewPassword.getText().toString().trim());


                BmobQuery<MyUser> query = new BmobQuery<MyUser>();
                query.addWhereEqualTo("password", md5OldPassword);
                query.findObjects(ChangePasswordActivity.this, new FindListener<MyUser>() {

                    @Override
                    public void onSuccess(List<MyUser> list) {
                        mProgressDialog.dismiss();
                        if (list.size() > 0) {
                            MyUser myUser1 = new MyUser();
                            myUser1.setPassword(Md5EtReNewPassword);
                            myUser1.update(ChangePasswordActivity.this, (String) SPUtils.get(ChangePasswordActivity.this, "objectId", ""), new UpdateListener() {
                                @Override
                                public void onSuccess() {

                                    Toast.makeText(ChangePasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                }

                                @Override
                                public void onFailure(int i, String s) {

                                    String message = BmobErrorCodeUtils.getMessage(i, s);
                                    Toast.makeText(ChangePasswordActivity.this, message, Toast.LENGTH_SHORT).show();
                                }
                            });
                        } else {
                            Toast.makeText(ChangePasswordActivity.this, "原密码错误，请重新输入", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(int i, String s) {
                        String message = BmobErrorCodeUtils.getMessage(i, s);
                        Toast.makeText(ChangePasswordActivity.this, message, Toast.LENGTH_SHORT).show();

                    }
                });


                break;

        }

    }


}
